<!--
 * @Descripttion: 出手鸭小程序
 * @version:1.0.1 
 * @Author: HHL
 * @Date: 2023-10-12 19:03:44
 * @LastEditTime: 2023-10-19 11:16:29
 * @LastEditors:Hui
-->

<template>
  <popup-bottom ref="popup" title="取消原因" @change="onChange">
    <template #default>

      <view>
        <view class="w100">
          <scroll-view scroll-y class="list">
            <view
              class="item flex flex-align-center"
              v-for="(item, index) in cancelOptions"
              :key="index"
              @click="handleSelect(index)"
            >
              <view class="p flex-1">{{item}}</view>
              <view
                class="icon bg-default"
                :class="index === activeIndex ? 'selected' : ''"  
              />
            </view>
          </scroll-view>
        </view>

        <view class="textarea_wrap">
          <textarea
            v-model="cancelReason"
            class="textarea_box"
            placeholder="其他原因"
            placeholder-class="textarea-placeholder"
          />
        </view>
        

        <view class="foot flex flex-between">
          <button
            class="btn btn2"
            @click="handleCancel"
          >
            取消订单 
          </button>
          <button
            class="btn btn1"
            @click="onClose"
          >
            继续回收
          </button>
        </view>
      </view>

    </template>
  </popup-bottom>
</template>

<script>
import { getByConfigKey, getOrderCancel } from '@/api'
import { waitShowToast } from '@/utils'

export default {
  props: {
    orderNo: { type: String, default: '' }
  },

  data() {
    return {
      activeIndex: -1,
      cancelOptions: [],
      cancelReason: '',
    }
  },

  methods: {
    onChange({ show }) {
      if (show) {
        this.activeIndex = -1
        this.cancelReason = ''
        if (this.cancelOptions.length === 0) {
          this.loadList()
        }
      }
      
    },
    loadList() {
      getByConfigKey('user_cancel_reason').then(({ data, code }) => {
        if (code === 200 && data.configType === 2 && data.configValue) {
          try {
            const arr = JSON.parse(data.configValue)
            this.cancelOptions = arr
          }catch{() => {}}
        }
      })
    },

    // 选择
    handleSelect(index) {
      this.activeIndex = index
    },


    // 确认取消
    handleCancel() {
      if (this.activeIndex === -1 && !this.cancelReason) {
        uni.showToast({ title: '请选择取消原因', mask: false, icon: 'none' })
        return
      }
      getOrderCancel({
        cancelReason: '客户端取消订单:' + (this.activeIndex !== -1 ? this.cancelOptions[this.activeIndex] : '') + this.cancelReason,
        orderNo: this.orderNo,
      }).then(({ code, msg }) => {
        if (code === 200) {
          this.$emit('loadDetail')
          waitShowToast('取消成功')
          this.onClose()
        } else {
          waitShowToast(msg)
        }
      })
    },

    onOpen() {
      this.$refs.popup.open()
    },

    onClose() {
      this.$refs.popup.close()
    },
  }
}
</script>


<style lang="scss" scoped>


.list{
  width: 100%;
  max-height: 415rpx;
  .item{
    margin-bottom: 20rpx;
    padding: 10rpx 30rpx;
    .p{
      font-size: 26rpx;
      font-weight: 500;
      color: #333333;
      line-height: 36rpx;
      padding-right: 50rpx;
    }
    .icon{
      flex-shrink: 0;
      width: 36rpx;
      height: 36rpx;
      background-image: url(@/static/images/icon/select2_1.png);
      &.selected{
        background-image: url(@/static/images/icon/select2_2.png);
      }
    }

  }
}

.textarea_wrap{
  padding: 20rpx 30rpx 0;
  .textarea_box{
    background: #FFFFFF;
    border-radius: 8rpx;
    width: 630rpx;
    display: block;
    height: 200rpx;
    padding: 14rpx 28rpx 30rpx;
    font-size: 26rpx;
    line-height: 40rpx;
    border: 1px solid #D9D9D9;
    border-radius: 8rpx;
  }

  .textarea-placeholder{
    color: #999;
    font-size: 26rpx;
    line-height: 40rpx;
  }
}

.foot{
  padding: 40rpx 40rpx 50rpx;
  .btn{
    width: 300rpx;
    border-radius: 12rpx;
    font-size: 30rpx;
  }

  .btn1{
    background: linear-gradient(85deg, #98E5FF, #00FFE1);
    line-height: 72rpx;
    color: #fff;
  }
  .btn2{
    border: 1px solid #D9D9D9;
    line-height: 70rpx;
    color: #999;
  }
}
</style>